//
//  ImagePanel.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-6-3.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "ImagePanel.h"

@interface ImageButton()
{
    XUIButton *_button;
    
    UIImageView *_deleteImageView;
}

@property(nonatomic,readonly) XUIButton *button;
@property(nonatomic,readonly) UIImageView *deleteImageView;

@end

@implementation ImageButton

@synthesize szTag;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.userInteractionEnabled = NO;
    
    _button = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCenterImage delegate:self];
    _button.frame = self.bounds;
    [self addSubview:_button];
    
    _deleteImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"headviewpanel_deletetag.png"]];
    _deleteImageView.hidden = YES;
    [self addSubview:_deleteImageView];
    
    return self;
}

-(void)layoutSubviews
{
    _button.frame = self.bounds;
    
    _deleteImageView.center = CGPointMake(_button.right, _button.top);
}

-(void)setHiddenDeleteTag:(BOOL)hiddenDeleteTag
{
    _deleteImageView.hidden = hiddenDeleteTag;
}

-(void)setImage:(UIImage *)image
{
    [_button SetImage:image];
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
//    [_delegate clickImageButton:self];
}

@end

NSUInteger const AddImageButtonTag = 10000;
NSUInteger const DeleteImageButtonTag = 20000;

@implementation ImagePanel

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    _contain = [NSMutableArray new];
    
    _controlEnable = YES;
    
    _hiddenDeleteTag = YES;
    
    _addImageBtn = [[ImageButton alloc]initWithFrame:CGRectMake(10, 10, frame.size.height - 10 - 10, frame.size.height - 10 - 10)];
//    _addImageBtn.delegate = self;
    _addImageBtn.image = [UIImage imageNamed:@"headviewpanel_add.png"];
    _addImageBtn.tag = AddImageButtonTag;
    [self addSubview:_addImageBtn];
    
    [_contain addObject:_addImageBtn];
    
    _deleteImageBtn = [[ImageButton alloc]initWithFrame:CGRectMake(_addImageBtn.right + 15, 10, frame.size.height - 10 - 10, frame.size.height - 10 - 10)];
//    _deleteImageBtn.delegate = self;
    _deleteImageBtn.image = [UIImage imageNamed:@"headviewpanel_delete.png"];
    _deleteImageBtn.tag = DeleteImageButtonTag;
    _deleteImageBtn.hidden = YES;
    [self addSubview:_deleteImageBtn];
    
    [_contain addObject:_deleteImageBtn];
    
    return self;
}

-(void)setControlEnable:(BOOL)controlEnable
{
    _controlEnable = controlEnable;
    
    _addImageBtn.hidden = YES;
    _deleteImageBtn.hidden = YES;
    
    [_addImageBtn removeFromSuperview];
    [_deleteImageBtn removeFromSuperview];
    
    [_contain removeObject:_addImageBtn];
    [_contain removeObject:_deleteImageBtn];
}

-(void)setHiddenDeleteTag:(BOOL)hiddenDeleteTag
{
    _hiddenDeleteTag = hiddenDeleteTag;
    
    _deleteMode = !hiddenDeleteTag;
}

-(NSUInteger)imageCount
{
    NSMutableArray *ar = [_contain mutableCopy];
    [ar removeObject:_addImageBtn];
    [ar removeObject:_deleteImageBtn];
    return ar.count;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size = CGSizeMake(self.height - 10 - 10, self.height - 10 - 10);
    
    CGFloat diffW = size.width - _addImageBtn.width;
    
    for(NSInteger i = 0;i < _contain.count; i++)
    {
        UIView *view = _contain[i];
        view.size = size;
        
        view.top = 10;
        
        if(i == 0)
        {
            view.left = 10;
        }
        else if(i > 0)
        {
            view.left += diffW;
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    for(ImageButton *imgBtn in _contain)
    {
        if(imgBtn.tag == AddImageButtonTag && _addImageBtn.hidden == NO)
        {
            if(CGRectContainsPoint(imgBtn.frame, [touch locationInView:self]))
            {
                [_delegate clickAddImageButtonOfImagePanel:self];
                break;
            }
        }
        else if(imgBtn.tag == DeleteImageButtonTag && _deleteImageBtn.hidden == NO)
        {
            if(CGRectContainsPoint(imgBtn.frame, [touch locationInView:self]))
            {
                for(ImageButton *imgBtn in _contain)
                {
                    if(imgBtn != _addImageBtn && imgBtn != _deleteImageBtn)
                    {
                        imgBtn.hiddenDeleteTag = _deleteMode;
                    }
                }
                
                _deleteMode = !_deleteMode;
                
                [_delegate clickDeleteImageButtonOfImagePanel:self];
                
                break;
            }
        }
        else if(imgBtn.tag != AddImageButtonTag && imgBtn.tag != DeleteImageButtonTag)
        {
            CGRect deleteTagFrame = [self convertRect:imgBtn.deleteImageView.frame fromView:imgBtn];
            if(CGRectContainsPoint(deleteTagFrame, [touch locationInView:self]) || CGRectContainsPoint(imgBtn.frame, [touch locationInView:self]))
            {
                if(_deleteMode == NO)
                {
                    if([_delegate respondsToSelector:@selector(imagePanel:didClickedImageButton:imageContains:)])
                    {
                        NSMutableArray *ar = [_contain mutableCopy];
                        [ar removeObject:_addImageBtn];
                        [ar removeObject:_deleteImageBtn];
                        [_delegate imagePanel:self didClickedImageButton:imgBtn imageContains:ar];
                    }
                }
                else
                {
                    [_delegate clickDeleteModeImageButtonOfImagePanel:self atTag:imgBtn.szTag];
                }
                break;
            }
        }
    }
}

-(void)addImage:(UIImage*)image withTag:(NSString*)tag
{
    [self exitDeleteMode];
    
    image = [UIImage imageFromImage:image size:_addImageBtn.size fillStyle:EImageFillStyleStretchByCenterScale];
    
    ImageButton *imgBtn = [[ImageButton alloc]initWithFrame:_addImageBtn.frame];
    imgBtn.szTag = tag;
    imgBtn.hiddenDeleteTag = _hiddenDeleteTag;
    imgBtn.image = image;
    [self addSubview:imgBtn];
    
    NSUInteger index = [_contain indexOfObject:_addImageBtn];
    if(index != NSNotFound)
        [_contain insertObject:imgBtn atIndex:index];
    else
        [_contain addObject:imgBtn];
    
    CGFloat x = 10;
    for(NSInteger i = 0; i < _contain.count;i++)
    {
        ImageButton *imgBtn = _contain[i];
        imgBtn.left = x;
        x += imgBtn.width + 15;
    }
    
    if(_contain.count >= 5)
    {
        _deleteImageBtn.frame = _addImageBtn.frame;
        _addImageBtn.hidden = YES;
    }
    
    _deleteImageBtn.hidden = NO;
}

-(void)replaceImage:(UIImage*)image withTag:(NSString*)tag
{
    image = [UIImage imageFromImage:image size:_addImageBtn.size fillStyle:EImageFillStyleStretchByCenterScale];
    for(NSInteger i = 0; i < _contain.count;i++)
    {
        ImageButton *imageBtn = _contain[i];
        if([imageBtn.szTag isEqualToString:tag])
        {
            imageBtn.image = image;
            break;
        }
    }
}

-(void)deleteImageWithTag:(NSString*)szTag
{
    for(NSInteger i = 0; i < _contain.count; i++)
    {
        ImageButton *imgBtn = _contain[i];
        if([imgBtn.szTag isEqualToString:szTag])
        {
            CGRect frame = imgBtn.frame;
            for(NSInteger j = i + 1; j< _contain.count ;j++)
            {
                ImageButton *imgBtn = _contain[j];
                CGRect f = imgBtn.frame;
                imgBtn.frame = frame;
                frame = f;
            }
            [imgBtn removeFromSuperview];
            [_contain removeObject:imgBtn];
            
            if(_contain.count <5)
                _addImageBtn.hidden = NO;
            break;
        }
    }
    
    if(_contain.count <= 2)
        _deleteImageBtn.hidden = YES;
    
    if(_contain.count <= 2)
        [self exitDeleteMode];
}

-(void)exitDeleteMode
{
    if(_hiddenDeleteTag == NO)
        return;
    
    for(ImageButton *imgBtn in _contain)
    {
        if(imgBtn != _addImageBtn && imgBtn != _deleteImageBtn)
        {
            imgBtn.hiddenDeleteTag = YES;
        }
    }
    
    _deleteMode = NO;
}

@end
